<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Model\Art;

class TupianController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
		//echo "jdlkfjdsk";die;
        $file = DB::table('file')->orderBy('id','desc')->paginate(5);
        return view("/admin/tupian.index")->with('file',$file);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

       return view('admin/tupian.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		$file = $request->file('name');
        $urls=[];
        foreach($file as $file){
            if($file->isValid()){
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                $path = 'upload/images/';
                $name = "origin_".time().rand(10,100).'.'.$ext;
                $path .=$name;
                //获取文件上传到服务器之后的临时路径  tmp_path
                $tmp_path = $file->getRealPath();
                //利用文件存储执行上传 put($path,file_get_contents($tmp_path)
               Storage::disk('myupload')->put($path,file_get_contents($tmp_path));             //把上传之后的url加入数组
                $urls[]= ['url'=>"/upload/".$path];
            }
        }

        DB::table('file')->insert($urls);
       // return redirect('image');
        //返回ajax

        $data =[
            'status'=>200,
            'msg'=>'成功',
            'data'=>$urls
        ];
        echo json_encode($data);
		
		
		
		
		
		//dd($return->file('name'));
       /* $this->validate($request,[
           'name'=>'required',
            'picture'=>'required',
            
        ]);
        $file['name']=$request->name;
        $file['picture']=$request->picture;
        
        DB::table('file')->insert($file);
        return redirect('admin/tupian'); */
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $uaers = DB::table('file')->where('id','=',$id)->first();
        return view('admin/tupian.add');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
         $user = DB::table('users')->where('id','=',$id)->delete();
          return view('admin/user.add');
    }
}
